/*
 * @Author: szx
 * @Date: 2021-11-02 13:22:53
 * @LastEditTime: 2021-11-02 13:36:27
 * @Description:
 * @FilePath: \leetcode\1-99\21\21.cpp
 */

#include <iostream>
#include <unordered_set>
using namespace std;
struct ListNode {
    int val;
    ListNode* next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
   public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* l3 = new ListNode(-1);
        ListNode* res = l3;
        while (l1 != nullptr && l2 != nullptr) {
            if (l1->val > l2->val) {
                l3->next = l2;
                l2 = l2->next;
            } else {
                l3->next = l1;
                l1 = l1->next;
            }
            l3 = l3->next;
        }
        // if (l1 != nullptr) {
        //     l3->next = l1;
        // }
        // if (l2 != nullptr) {
        //     l3->next = l2;
        // }
        // 或者直接精简成下面这种写法
        l3->next = l1 == nullptr ? l2 : l1;

        return res->next;
    }
};